<template>
  <div class="rightContent">
    <div>
      <!-- 头部面包屑 -->
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>学生列表</el-breadcrumb-item>
      </el-breadcrumb>
    </div>

    <!-- 设置卡片 -->
    <el-card class="box-card" shadow="never">
      <el-row :gutter="20" class="addUser">
        <!-- 设置搜索框 -->
        <el-col :span="10">
          <el-input placeholder="请输入内容">
            <el-button slot="append" icon="el-icon-search"></el-button>
          </el-input>
        </el-col>
        <!-- 添加用户信息 -->
        <el-col :span="4">
          <el-button class="addUserBtn" type="primary" @click= "addUser">添加</el-button>
        </el-col>
      </el-row>
      <!-- 卡片里的表 -->

      <el-table :data="tableData" style="width:100%" stripe border>
        <!-- <el-table-column prop="stu_id" label="编号" width="80"></el-table-column> -->
        <el-table-column prop="name" label="姓名"> </el-table-column>
        <el-table-column prop="stu_no" label="学号"> </el-table-column>
        <!-- <el-table-column prop="year" label="入学年份"> </el-table-column>
        <el-table-column prop="maj_name" label="专业"> </el-table-column>
        <el-table-column prop="mail" label="邮箱"> </el-table-column>
        <el-table-column prop="course_level" label="课程等级"></el-table-column>
        <el-table-column prop="degree" label="学位"> </el-table-column> -->
        <el-table-column prop="create_date" label="创建日期"> </el-table-column>
        <el-table-column prop="modify_date" label="修改日期"> </el-table-column>
        <el-table-column label="激活状态" width="100">
          <template slot-scope="scope">
            <el-switch
              v-model="scope.row.active"
              active-color="#13ce66"
              inactive-color="#ff4949"
              @change="switchEvent(scope.row)"
            >
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="100">
          <template slot-scope="scope">
            <el-button @click="editEvent(scope.row)" type="primary" size="mini" plain>编辑</el-button>
            <el-button type="danger" size="mini" plain>删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <!-- 设置分页 -->
    <el-pagination
      :current-page= "currentpage"
      :page-sizes= "[5, 10, 15, 20]"
      :page-size= "getStudentsModel.page_size"
      layout= "total, sizes, prev, pager, next, jumper"
      :total = "totalCount"
      @current-change = "handleCurrentChange"
      @size-change = "handleSizeChange"
    >
    </el-pagination>
    <!-- 初始化录入信息对话框 -->
    <el-dialog title="添加数据" :visible.sync="dialogFormVisible">
      <el-form
        :model="form"
        label-position="left"
        style="width:80%"
        label-width="100px"
      >
        <el-form-item label="姓名" :label-width="formLabelWidth">
          <el-input
            v-model="form.name"
            autocomplete="off"
            size="small"
            maxlength="10"
          ></el-input>
        </el-form-item>
        <el-form-item label="学号" :label-width="formLabelWidth">
          <el-input autocomplete="off" size="small" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="入学年份" :label-width="formLabelWidth">
          <el-input autocomplete="off" size="small" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="专业" :label-width="formLabelWidth">
          <el-input autocomplete="off" size="small" maxlength="25"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" :label-width="formLabelWidth">
          <el-input autocomplete="off" size="small" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="课程等级" :label-width="formLabelWidth">
          <el-input autocomplete="off" size="small" maxlength="30"></el-input>
        </el-form-item>
        <el-form-item label="学位" :label-width="formLabelWidth">
          <el-input autocomplete="off" size="small" maxlength="30"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisible = false"
          >确 定</el-button
        >
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 获取列表参数对象
      getStudentsModel: {
        token: "",
        name: "",
        stu_no: "",
        page_count: 0,
        page_size: 10
      },
      // 更新学生状态参数对象
      upStudentStatus: {
        id: "",
        token: ""
      },
      changeModel: {},
      switchStatus: false,
      // 声明对话框状态
      dialogFormVisible: false,
      form: {
        name: "",
        region: "",
        date1: "",
        date2: "",
        delivery: false,
        type: [],
        resource: "",
        desc: ""
      },
      formLabelWidth: "120px",
      tableData: [],
      totalCount:0,
      currentpage:1,
    };
  },
  methods: {
    //添加用户对话框
    addUser() {
      console.log("我来添加数据了");
      this.dialogFormVisible = true;
      // 添加数据不需要请求参数
      this.$router.push({name:'Upload',query:{source:'add'}})
    },

    editEvent(item) {
      //传递参数给下个页面请求使用
      this.$router.push({name:'Upload',query:{source:'edit',stuid:item.stu_id}})
    },
    // 获取列表数据
    async getUserListData() {
      // //获取token数据
      const token = window.sessionStorage.getItem("token");
      // //给定义的数据赋值操作
      this.getStudentsModel.token = token;
      //准备url  请求参数   回调函数
      var getStuListUrl = this.$urls.StudentList;
      const { data: res } = await this.$http1(getStuListUrl, this.getStudentsModel);
      console.log(res);
      if(res.Code !== 10000) return this.$message.error(res.Message);
      console.log(
        "-------------------------打印结果----------------------------------"
      );
      var datas = res.Result.Data;
      this.tableData = datas;
      this.totalCount = res.Result.total_count;
    },
    // 定义点击更新状态的事件
    async switchEvent(model) {
      console.log("点击更新状态model.name == " + model.name);
      this.upStudentStatus.token = window.sessionStorage.getItem("token");
      this.upStudentStatus.id = model.stu_id;
      // 发起请求更新学生状态
      const { data: res } = await this.$http1(
        this.$urls.UpStuStatus,
        this.upStudentStatus
      );
      console.log(res);
      if (res.Code !== 10000) return this.$message.error("状态激活失败");
      this.$message.success("激活成功");
    },
    // 改变当前页码
    handleCurrentChange(val) {
      console.log('当前第'+ val + '页');
      this.currentpage = val;
      this.getStudentsModel.page_size = this.currentpage - 1;
      this.getUserListData();
    },
    //改变一页多少条数据
    handleSizeChange(val) {
      console.log('每页'+ val + '条数据');
      this.getStudentsModel.page_size = val;
      this.getUserListData();
    }
  },
  // 创建模板之前
  beforeCreate() {
    console.log("我来了------beforeCreate----");
  },
  // 创建模板完成
  created() {
    console.log("我来了------created----");
    this.getUserListData();
  },

  beforeMount() {
    console.log("我来了------beforeMount----");
  },
  mounted() {
    console.log("我来了------mounted----");
  }
};
</script>

<style>
.rightContent {
  /* padding: 8px; */
  background-color: #EAEDF2;
}
.box-card {
  margin: 15px 0;
  box-shadow: 0 1px 1px rgba(0, 0, 0, 0.15);
}
.addUser {
  margin: 15px;
}
.addUserBtn {
  padding: 12px 50px;
}
.el-pagination {
  margin-top: 15px;
}
</style>
